#!/bin/bash/python
# _*_ coding: utf-8 _*_
# file： tool_get_remote_file
# date-time: 2023/8/29-18:12
# auth:hard work
# description
import os
import time

from common import remote_device_cfg
from tools_dev_and_sys.tool_ssh_base import SSH_Server

class Ftp_tools(SSH_Server):
    def __init__(self, ip, usrname, passwd, port):
        super().__init__(ip, usrname,passwd,port)
        self._ip = ip
        self._username = usrname
        self._passwd = passwd
        self._port = port
        self.client = SSH_Server(self._ip, self._username, self._passwd, self._port)
        self.client.connect()
        self.client.open_sftp()

    def get_os_file(self, remote_dir, local_dir):
        print("test dut is : ",self._ip)
        try:
            print("ftp get doing ...")
            self.client.sftp_get(remotefile=remote_dir, localfile=local_dir)
            time.sleep(5)
            if os.path.exists(local_dir):
                print("get success")
                self.client.close()
                return True
            else:
                print("get faild")
        except Exception as e:
            print(e)
            raise "read and write error !!!"
        self.client.close()

    def put_local_file_to_os(self, local_dir, remote_dir, file_name):
        print("ftp put doing ...")
        try:
            # self.client.open_sftp()
            self.client.sftp_put(localfile=local_dir, remotefile=remote_dir)
            time.sleep(15)
        except Exception:
            raise 'ftp error'
        self.client.get_prompt(expect_symbol='# ')
        self.client.channel_send_cmd(cmd='cd /app/', break_prompt='# ')
        ret = self.client.channel_send_cmd(cmd='find -name {}'.format(file_name), break_prompt='# ')
        self.client.close()
        if file_name in ret:
            print("put success")
            return True
        else:
            raise "put file error !!!"


if __name__ == '__main__':
    local_dir = r'C:\version\GW\systemlog.log'
    remote_dir = '/app/usrfs/cfile/ifm/systemlog.log'
    # client =''
    # put_local_file_to_os(client,local_dir, remote_dir)
    _ftp = Ftp_tools(ip='10.11.3.232',usrname='root',passwd='hengshan',port=22)
    _ftp.get_os_file(remote_dir, local_dir)
